# -*- coding: utf-8 -*-

from PyQt4 import QtGui,QtCore
from chat_window import Ui_chat_window
from logic import thread_msgs
from ctypes import *

class ChatMainwindow(QtGui.QMainWindow):
    def __init__(self, logic):
        QtGui.QMainWindow.__init__(self)
        self.logic = logic        
        
    def closeEvent(self, event):
        self.logic.on_close()
    
class ChatUI(QtCore.QObject):
    def __init__(self, logicthread):
        QtCore.QObject.__init__(self)
        self.logicthread = logicthread
        self.clientinstance = None
        self.mainwindow = ChatMainwindow(self)
        self.chat_window = Ui_chat_window()
        self.chat_window.setupUi(self.mainwindow)
        self.chat_window.btn_ptt.setText(u"按住说话")
        QtCore.QObject.connect(self,QtCore.SIGNAL("show_sig(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)"), 
                               self.show_slot)        
        self.chat_window.btn_ptt.pressed.connect(self.ptt_press)
        self.chat_window.btn_ptt.released.connect(self.ptt_release)
                  
        fileName = "iprs_cpp_interface.dll"
        self.poc_mod = cdll.LoadLibrary(fileName)
        if (self.poc_mod):
            self.poc_mod.init_mod()
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.on_timer)
        self.timer.start(100)        
        
    def show(self, clientinstance, sceneid, deskid, sessionid, useritem):
        self.emit(QtCore.SIGNAL('show_sig(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)'),
                  clientinstance, sceneid, deskid, sessionid, useritem)        
        
    def show_slot(self, clientinstance, sceneid, deskid, sessionid, useritem):
        self.clientinstance = clientinstance
        self.chat_window.label_distance.setText(u"距离："+useritem['distance']+u"米")
        self.chat_window.label_nick.setText(u"昵称："+useritem['nick'])
        self.chat_window.label_sex.setText(u"性别 ："+useritem['sex'])
        self.chat_window.edit_recv.clear()
        self.chat_window.edit_send.clear()
        self.partner = useritem
        self.sessionid = sessionid
        if (self.poc_mod):
            str_sessionid = c_char_p()
            str_myid = c_char_p()
            str_sessionid.value = str(self.sessionid)            
            str_myid.value = str(self.clientinstance.jid.node)
            ret = self.poc_mod.join_poc_talk(str_sessionid, str_myid)
            print ("join poc ret %r" % ret)
        self.mainwindow.show()
        
    def on_close(self):
        if (self.poc_mod):
            self.poc_mod.leave_poc_talk()
            
    def on_timer(self):
        if (self.poc_mod):
            enable = self.poc_mod.is_ptt_enabled()
            if (enable==1):
                self.chat_window.btn_ptt.setEnabled(True)
                if (self.poc_mod.is_self_talking()):                    
                    self.chat_window.btn_ptt.setText(u"您正在说话")
                else:                    
                    self.chat_window.btn_ptt.setText(u"按住说话")
            else:
                self.chat_window.btn_ptt.setText(u"对方正在说话")
        
    def ptt_press(self):        
        if (self.poc_mod):
            self.poc_mod.start_ptt()        
    
    def ptt_release(self):        
        if (self.poc_mod):
            self.poc_mod.end_ptt()
    